package org.zhangyp.n03algorithm.n03Sort;

import java.io.*;

/**
 * 各种排序时长测试
 *
 * @author ypzhang
 * @since 2024/12/26
 */
public class Test {
    public static Double[] read200WData() {
        String fileName = "F:\\my\\200w.txt";
        InputStreamReader isr = null;
        BufferedReader br = null;
        try {
            isr = new InputStreamReader(new FileInputStream(fileName), "UTF-8");
            br = new BufferedReader(isr);
            Double data[] = new Double[2100001];
            int i = 0;
            String str = null;
            while ((str = br.readLine()) != null) {
                data[i++] = Double.valueOf(str);
            }
            return data;
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    public static void write(String fileName, Double[] data) {
        File file = new File("F:\\my\\out\\" + fileName);
        Writer out = null;
        try {
            out = new FileWriter(file);
            for (int i = 0; i < data.length; i++) {
                out.write(data[i] + "\r\n");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            try {
                out.close();
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        Double[] data = read200WData();
        System.out.println("数据读取完成，耗时:" + (System.currentTimeMillis() - start) + "ms");
        start = System.currentTimeMillis();
        ASort.mergeSort(data);
        System.out.println("归并排序消耗的时间为:" + (System.currentTimeMillis() - start) + "ms");
        //write("200w-mergeSort.txt", data);

       /* start = System.currentTimeMillis();
        data = read200WData();
        System.out.println("数据读取完成，耗时:" + (System.currentTimeMillis() - start) + "ms");
        start = System.currentTimeMillis();
        ASort.insertSort(data);
        System.out.println("插入排序消耗的时间为:" + (System.currentTimeMillis() - start) + "ms");*/
        //插入排序基本排不出来

        start = System.currentTimeMillis();
        data = read200WData();
        System.out.println("数据读取完成，耗时:" + (System.currentTimeMillis() - start) + "ms");
        start = System.currentTimeMillis();
        BSort.quiklySort(data);
        System.out.println("快速排序消耗的时间为:" + (System.currentTimeMillis() - start) + "ms");
        //write("200w-quiklySort.txt", data);

        /*start = System.currentTimeMillis();
        data = read200WData();
        System.out.println("数据读取完成，耗时:" + (System.currentTimeMillis() - start) + "ms");
        start = System.currentTimeMillis();
        BSort.bubbleSort(data);
        System.out.println("冒泡排序消耗的时间为:" + (System.currentTimeMillis() - start) + "ms");*/

        // 冒泡排序基本也排不出来

    }


}
